package com.yegs.quartz.job;

import com.yegs.quartz.QuartzApplication;
import com.yegs.quartz.mysqlEntity.User;
import com.yegs.quartz.service.impl.UserServiceImpl;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 例子：定义一个执行输出语句的任务
 * @DisallowConcurrentExecution : 此标记用在实现Job的类上面,意思是不允许并发执行.
 * 注org.quartz.threadPool.threadCount的数量有多个的情况,@DisallowConcurrentExecution才生效
 *
 */
@DisallowConcurrentExecution
@Component
public class HelloJob implements Job {

    @Autowired
    private UserServiceImpl userService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("Hello执行：" + LocalDateTime.now());
        userService = (UserServiceImpl) QuartzApplication.applicationContext.getBean("userService");
        List<User> all = userService.findAll();
        System.out.println(all);
    }
}